package com.longjianghuasheng.logistics.business.order.fragment

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.order.viewmodel.DriverOrderViewModel
import com.longjianghuasheng.logistics.databinding.FragmentDriverOrderOutBinding
import com.longjianghuasheng.logistics.eventViewModel

class DriverOrderOutFragment : BaseFragment<DriverOrderViewModel, FragmentDriverOrderOutBinding>() {
    private val titles by lazy {

        resources.getStringArray(R.array.driver_order_tab)
    }
    private val fragments by lazy {
        arrayListOf<Fragment>()
    }
    private val pagerAdapter by lazy {
        OrderOutPagerAdapter(this)
    }

    override fun layoutId(): Int = R.layout.fragment_driver_order_out

    override fun initView(savedInstanceState: Bundle?) {
        initTitle()

        mDatabind.initTabAndViewPager()
    }

    private fun FragmentDriverOrderOutBinding.initTabAndViewPager() {


        viewPager.run {
            isUserInputEnabled = false

            offscreenPageLimit = titles.size

            adapter = pagerAdapter



            tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {


                    setCurrentItem(tab?.position ?: 0, false)

                    eventViewModel.onOrderDriverSelectEvent.value = tab?.position ?: 0

                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {

                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }
            })

        }

        titles.forEachIndexed { index, s ->

            tabLayout.addTab(tabLayout.newTab().setText(s))
            fragments.add(DriverOrderFragment.newInstance(index))

        }



        tabLayout.getTabAt(arguments?.getInt("position") ?: 0)
            ?.select()


    }

    @SuppressLint("NewApi", "UseCompatTextViewDrawableApis")
    private fun initTitle() {
        mDatabind.include.titleBar.run {

            leftText.compoundDrawableTintList = ColorStateList.valueOf(Color.WHITE)

            setTitle("我的订单").centerText.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

            setLeftClickListener {
                navigateUp()
            }
        }


    }

    override fun createObserver() {
        super.createObserver()
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    inner class OrderOutPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {


        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]

    }
}